local skynet = require "skynet"
local logicmng = require "logicmng"
local tabletool = require "tabletool"
local timetool = require "timetool"
local msghelper = require "tablehelper"
local timer = require "timer"
local filelog = require "filelog"
local msgproxy = require "msgproxy"
local base = require "base"
local playerdatadao = require "playerdatadao"
local configdao = require "configdao"
require "enum"
local RoomTableLogic = {}

function RoomTableLogic.init(tableobj, conf, roomsvr_id)
	if conf == nil or roomsvr_id == nil then
		filelog.sys_error("RoomTableLogic.init conf == nil", conf, roomsvr_id)
		return false
	end

	local roomseatlogic = logicmng.get_logicbyname("roomseatlogic")
	tableobj.id = conf.id
	tableobj.svr_id = roomsvr_id

	if conf.gamecfg.max_player_num ~= nil then
		conf.max_player_num = conf.gamecfg.max_player_num
	end

	if conf.gamecfg.base_score ~= nil then
		conf.base_score = conf.gamecfg.base_score
	end

	if conf.gamecfg.min_carry_coin ~= nil then
		conf.min_carry_coin = conf.gamecfg.min_carry_coin
	end	

	local seatobj = require("object.seatobj")
	local seat
	local count = 1
    while count <= conf.max_player_num do
    	seat = seatobj:new({
    		--Add 座位其他变量
    		timeout_count = 0, --超时次数
    		--[[
				EWinResult = {
					WIN_RESULT_UNKNOW = 0,
					WIN_RESULT_WIN = 1,
					WIN_RESULT_LOSE = 2,
				}
    		]]
    		win = EWinResult.WIN_RESULT_UNKNOW, --表示玩家当前是胜利还是失败
    		coin = 0,    --玩家金币
    		lottery = 0, --奖券
    		roomcard = 0, --玩家身上的房卡
    		gemstone = 0, --玩家身上的宝石
			is_disconnected = false,   --玩家是否断线
			cards = {},            --记录玩家发牌后的手牌
			cards_tricks = {},     --麻将牌墩 
			card_type = 0,         --记录当前牌型
			curr_score = 0,        --当前得分
			total_score = 0,       --总得分
			is_leave = false,      --是否离开
			is_banker = false,     --是否庄家
			win_num = 0,           --记录玩家胜利次数
			fail_num = 0,          --记录玩家失败次数
			draw_num = 0,          --记录玩家平局次数
			hupai_card = 0,        --玩家胡牌的牌 0表示无
			fan_types = {},   	   --翻型数组，如果无就是空数组
			fan_value = 0,   	   --总翻值 
    	})
    	roomseatlogic.init(seat, count)
    	table.insert(tableobj.seats, seat) 
		count = count + 1
    end

    tableobj.conf = tabletool.deepcopy(conf)
	local roomgamelogic = msghelper:get_game_logic()
	local game = require("object.gameobj")
	tableobj.gamelogic = game:new()
	roomgamelogic.init(tableobj.gamelogic, tableobj)

	if conf.retain_time ~= nil and conf.retain_time > 0 then
		if tableobj.delete_table_timer_id > 0 then
			timer.cleartimer(tableobj.delete_table_timer_id)
			tableobj.delete_table_timer_id = -1
		end
    	tableobj.delete_table_timer_id = timer.settimer(conf.retain_time*100, "delete_table")
		tableobj.retain_to_time = timetool.get_time() + conf.retain_time
    end
    tableobj.retain_to_num = conf.retain_to_num
    tableobj.state = ETableState.TABLE_STATE_WAIT_PLAYER_SITDOWN
    roomgamelogic.run(tableobj.gamelogic)
	return true
end

function RoomTableLogic.clear(tableobj)
	if tableobj.timer_id > 0 then
		timer.cleartimer(tableobj.timer_id)
		tableobj.timer_id = -1
	end

	if tableobj.delete_table_timer_id > 0 then
		timer.cleartimer(tableobj.delete_table_timer_id)
		tableobj.delete_table_timer_id = -1
	end

	if tableobj.disband_table_timer_id > 0 then
		timer.cleartimer(tableobj.disband_table_timer_id)
		tableobj.disband_table_timer_id = -1		
	end

	for k,v in pairs(tableobj) do
		tableobj[k] = nil
	end
end

--[[
	seat: nil表示否， 非nil表示是
]]
function RoomTableLogic.entertable(tableobj, request, seat)
	if seat and seat.is_tuoguan then
		seat.is_tuoguan = false

		local noticemsg = {
			rid = seat.rid,
			roomsvr_seat_index = seat.index,	
			is_leave = false,
		}
		msghelper:sendmsg_to_alltableplayer("PlayerLeaveNtc", noticemsg)
		if not tableobj.is_had_start then
			if tableobj.delete_table_timer_id > 0 then
				timer.cleartimer(tableobj.delete_table_timer_id)
				tableobj.delete_table_timer_id = -1
			end
		end
	else
		local waitinfo = tableobj.waits[request.rid]
		if waitinfo == nil then
			tableobj.waits[request.rid] = {}
			waitinfo = tableobj.waits[request.rid]
			waitinfo.playerinfo = {}
			tableobj.waits[request.rid] = waitinfo
			tableobj.cur_watch_playernum = tableobj.cur_watch_playernum + 1			 
		end
		waitinfo.rid = request.rid
		waitinfo.gatesvr_id = request.gatesvr_id
		waitinfo.agent_address = request.agent_address
		waitinfo.coin = request.coin
		waitinfo.roomcard = request.roomcard
		waitinfo.lottery = request.lottery
		waitinfo.gemstone = request.gemstone
		waitinfo.playerinfo.rolename=request.playerinfo.rolename
		waitinfo.playerinfo.logo=request.playerinfo.rolename
		waitinfo.playerinfo.sex=request.playerinfo.sex
		waitinfo.playerinfo.ip=request.playerinfo.ip
	end
	if tableobj.delete_table_timer_id > 0 then
		timer.cleartimer(tableobj.delete_table_timer_id)
		tableobj.delete_table_timer_id = -1
	end
end
--- 断线重连处理函数
-- @param tableobj
-- @param request
-- @param seat
--
function RoomTableLogic.reentertable(tableobj, request, seat)
	if not RoomTableLogic.is_onegameend(tableobj) then
		local player_action
		local noticemsg = {
			rid = seat.rid,
			roomsvr_seat_index = seat.index,
			cards = seat.cards,   
		}
		msghelper:sendmsg_to_tableplayer(seat, "NNDealCardsNtc", noticemsg)
	end

	if seat.is_tuoguan then
		if seat.is_disconnected then
			seat.is_disconnected = false
		end
		seat.is_tuoguan = false
		local noticemsg = {
			rid = seat.rid,
			roomsvr_seat_index = seat.index,	
			is_leave = false,
		}
		msghelper:sendmsg_to_alltableplayer("PlayerLeaveNtc", noticemsg)
	end

	local roomseatlogic = logicmng.get_logicbyname("roomseatlogic")
	local playerleavemsg = {}
	for _, tmpseat in ipairs(tableobj.seats) do
		if not roomseatlogic.is_empty(tmpseat) and tmpseat.is_tuoguan then
			playerleavemsg.rid = tmpseat.rid
			playerleavemsg.roomsvr_seat_index = tmpseat.index	
			playerleavemsg.is_leave = true
			msghelper:sendmsg_to_tableplayer(seat, "PlayerLeaveNtc", playerleavemsg)
		end
	end

	if tableobj.playerdisbandlist ~= nil then
		local noticemsg = {
	    	rid = tableobj.seats[tableobj.applydisband_seat_index].rid,
			rolename = tableobj.seats[tableobj.applydisband_seat_index].playerinfo.rolename,
			logo = tableobj.seats[tableobj.applydisband_seat_index].playerinfo.logo,
			is_had_start = 1,
			playerdisbandlist = tableobj.playerdisbandlist,
			apply_disband_countdown = configdao.get_businessconfitem_by_index(100, 1000, "globalvarcfg",   "apply_disband_countdown"),
		}
		noticemsg.surplus_applydisband_countdown = noticemsg.apply_disband_countdown - (timetool.get_time() - tableobj.apply_disband_time)
		msghelper:sendmsg_to_tableplayer(seat, "ApplyDisbandNtc", noticemsg)	
	end

	if tableobj.state == ETableState.TABLE_STATE_WAIT_ALL_READY 
		and seat.state == ESeatState.SEAT_STATE_WAIT_READY then
		--seat.state = ENNSeatState.SEAT_STATE_WAIT_START
		--RoomTableLogic.gameready(tableobj, nil, seat)
	end
end
--- 被动离开桌子
-- @param tableobj
-- @param rid
-- @param is_sendto_client
--被动离开桌子，使用该接口时玩家必须是在旁观中
--记住使用者如果循环遍历旁观队列一定要使用原队列的copy队列
function RoomTableLogic.passive_leavetable(tableobj, rid, is_sendto_client)
	local leavetablemsg = {
		roomsvr_id = tableobj.svr_id,
		roomsvr_table_id = tableobj.id,
		roomsvr_table_address = skynet.self(),
		is_sendto_client = is_sendto_client,
		gameid = tableobj.conf.gameid,
	}
	if tableobj.waits[rid] ~= nil then
		msghelper:sendmsg_to_waitplayer(tableobj.waits[rid], "leavetable", leavetablemsg)
		tableobj.cur_watch_playernum = tableobj.cur_watch_playernum - 1
	end
	tableobj.waits[rid] = nil

	if tableobj.isdeleteing then
		return
	end

	if tableobj.is_had_start then
		if tableobj.curr_to_num == 1 
			and not tableobj.is_onegameend then
			if RoomTableLogic.get_sitdown_player_num(tableobj) <= 0 then
				if tableobj.delete_table_timer_id > 0 then
					timer.cleartimer(tableobj.delete_table_timer_id)
					tableobj.delete_table_timer_id = -1
				end
 				tableobj.delete_table_timer_id = timer.settimer(200, "delete_table", {is_passive_disband=true, create_user_rid = tableobj.conf.create_user_rid})
			end
			return			
		end

		if RoomTableLogic.get_sitdown_player_num(tableobj) <= 0 then
			if tableobj.delete_table_timer_id > 0 then
				timer.cleartimer(tableobj.delete_table_timer_id)
				tableobj.delete_table_timer_id = -1
			end
			tableobj.delete_table_timer_id = timer.settimer(200, "delete_table")
		end
	else
		if RoomTableLogic.get_sitdown_player_num(tableobj) <= 0 or tableobj.cur_watch_playernum <= 0 then
			if tableobj.delete_table_timer_id > 0 then
				timer.cleartimer(tableobj.delete_table_timer_id)
				tableobj.delete_table_timer_id = -1
			end
			tableobj.delete_table_timer_id = timer.settimer(tableobj.conf.retain_time*100, "delete_table", {is_passive_disband=true, create_user_rid = tableobj.conf.create_user_rid})
		end
	end
end

function RoomTableLogic.leavetable(tableobj, request, seat)
	tableobj.waits[request.rid] = nil
	tableobj.cur_watch_playernum = tableobj.cur_watch_playernum - 1
	if tableobj.is_had_start then
		if tableobj.curr_to_num == 1 
			and not tableobj.is_onegameend then
			if RoomTableLogic.get_sitdown_player_num(tableobj) <= 0 then
				if tableobj.delete_table_timer_id > 0 then
					timer.cleartimer(tableobj.delete_table_timer_id)
					tableobj.delete_table_timer_id = -1
				end
 				tableobj.delete_table_timer_id = timer.settimer(200, "delete_table", {is_passive_disband=true, create_user_rid = tableobj.conf.create_user_rid})
			end
			return			
		end

		if RoomTableLogic.get_sitdown_player_num(tableobj) <= 0 then
			if tableobj.delete_table_timer_id > 0 then
				timer.cleartimer(tableobj.delete_table_timer_id)
				tableobj.delete_table_timer_id = -1
			end
			tableobj.delete_table_timer_id = timer.settimer(200, "delete_table")
		end
	else
		if RoomTableLogic.get_sitdown_player_num(tableobj) <= 0 or tableobj.cur_watch_playernum <= 0 then
			if tableobj.delete_table_timer_id > 0 then
				timer.cleartimer(tableobj.delete_table_timer_id)
				tableobj.delete_table_timer_id = -1
			end
			tableobj.delete_table_timer_id = timer.settimer(tableobj.conf.retain_time*100, "delete_table", {is_passive_disband=true, create_user_rid = tableobj.conf.create_user_rid})
		end
	end
end

function RoomTableLogic.sitdowntable(tableobj, request, seat)
	tableobj.waits[request.rid] = nil
	tableobj.cur_watch_playernum = tableobj.cur_watch_playernum - 1
	seat.rid = request.rid
	seat.gatesvr_id=request.gatesvr_id
	seat.agent_address = request.agent_address
	seat.playerinfo.rolename=request.playerinfo.rolename
	seat.playerinfo.logo=request.playerinfo.logo
	seat.playerinfo.sex=request.playerinfo.sex
	seat.playerinfo.ip = request.playerinfo.ip
	seat.state = ESeatState.SEAT_STATE_WAIT_READY
	seat.coin = request.coin
	seat.lottery = request.lottery
	seat.roomcard = request.roomcard
	seat.gemstone = request.gemstone
	seat.is_disconnected = false

	local noticemsg = {
		rid = seat.rid,
		seatinfo = {},
		tableplayerinfo = {},
		gameid = request.gameid,
	}
	msghelper:copy_seatinfo(noticemsg.seatinfo, seat)
	msghelper:copy_tableplayerinfo(noticemsg.tableplayerinfo, seat)
	msghelper:sendmsg_to_alltableplayer("SitdownTableNtc", noticemsg)

	if seat.is_tuoguan then
		seat.is_tuoguan = false
		seat.is_disconnected = false
		local noticemsg = {
			rid = seat.rid,
			roomsvr_seat_index = seat.index,	
			is_leave = false,
		}
		msghelper:sendmsg_to_alltableplayer("PlayerLeaveNtc", noticemsg)
	end

	tableobj.state = ETableState.TABLE_STATE_WAIT_ALL_READY

	local roomgamelogic = logicmng.get_logicbyname("roomgamelogic")
	roomgamelogic.onsitdowntable(tableobj.gamelogic, seat)

	msghelper:report_table_state()
end

function RoomTableLogic.passive_standuptable(tableobj, request, seat, reason, is_sendto_client)
	local roomgamelogic = msghelper:get_game_logic()
    local roomseatlogic = logicmng.get_logicbyname("roomseatlogic")
	local table_data = tableobj

	if not RoomTableLogic.is_onegameend(tableobj) then
		return
	end

	tableobj.sitdown_player_num = tableobj.sitdown_player_num - 1 

	local standuptablemsg = {
		rid = seat.rid,
		roomsvr_id = table_data.svr_id,
		roomsvr_seat_index = seat.index,
		roomsvr_table_id = table_data.id,
		gameid = tableobj.conf.gameid,
	}

	msghelper:sendmsg_to_tableplayer(seat, "standuptable", standuptablemsg)

	local noticemsg = {
		rid = seat.rid, 
		roomsvr_seat_index = seat.index,
		state = ESeatState.SEAT_STATE_NO_PLAYER,
		reason = reason,
		gameid = tableobj.conf.gameid,
	}

	if not is_sendto_client then
		msghelper:sendmsg_to_alltableplayer("StandupTableNtc", noticemsg)
	end

	seat.state = ESeatState.SEAT_STATE_NO_PLAYER

	if tableobj.waits[seat.rid] == nil then

		local waitinfo = {
			playerinfo = {},
		}
		tableobj.waits[seat.rid] = waitinfo

		waitinfo.rid = seat.rid
		waitinfo.gatesvr_id = seat.gatesvr_id
		waitinfo.agent_address = seat.agent_address
		waitinfo.playerinfo.rolename=seat.playerinfo.rolename
		waitinfo.playerinfo.logo=seat.playerinfo.rolename
		waitinfo.playerinfo.sex=seat.playerinfo.sex
		tableobj.cur_watch_playernum = tableobj.cur_watch_playernum + 1
	end
	---
	--初始化座位数据
	roomgamelogic.standup_clear_seat(tableobj.gamelogic, seat)

	if reason ~= EStandupReason.STANDUP_REASON_DELETE_TABLE then
		msghelper:report_table_state()
	end
end

function RoomTableLogic.standuptable(tableobj, request, seat)
	local roomgamelogic = msghelper:get_game_logic()

	if not RoomTableLogic.is_onegameend(tableobj) then
		seat.is_tuoguan = false
		return
	end
	tableobj.sitdown_player_num = tableobj.sitdown_player_num - 1
	local noticemsg = {
		rid = seat.rid, 
		roomsvr_seat_index = seat.index,
		state = ESeatState.SEAT_STATE_NO_PLAYER,
		reason = EStandupReason.STANDUP_REASON_ONSTANDUP,
		gameid = request.gameid,
	}
	if tableobj.state == ETableState.TABLE_STATE_UNKNOW then
		msghelper:sendmsg_to_tableplayer(seat, "StandupTableNtc", noticemsg)
	else
		msghelper:sendmsg_to_alltableplayer("StandupTableNtc", noticemsg)
	end
	seat.state = ESeatState.SEAT_STATE_NO_PLAYER
	if tableobj.waits[seat.rid] == nil then
		local waitinfo = {
			playerinfo = {},
		}
		tableobj.waits[seat.rid] = waitinfo
		waitinfo.rid = seat.rid
		waitinfo.gatesvr_id = seat.gatesvr_id
		waitinfo.agent_address = seat.agent_address
		waitinfo.playerinfo.rolename=seat.playerinfo.rolename
		waitinfo.playerinfo.logo=seat.playerinfo.rolename
		waitinfo.playerinfo.sex=seat.playerinfo.sex
		tableobj.cur_watch_playernum = tableobj.cur_watch_playernum + 1
	end
	--初始化座位数据
	roomgamelogic.standup_clear_seat(tableobj.gamelogic, seat)
	msghelper:report_table_state()
end

function RoomTableLogic.startgame(tableobj, request)
	if RoomTableLogic.is_canstartgame(tableobj) then
		local roomgamelogic = logicmng.get_logicbyname("roomgamelogic")
		tableobj.state = ETableState.TABLE_STATE_GAME_START
		roomgamelogic.run(tableobj.gamelogic)
	else
		--table_data.state = ENNTableState.TABLE_STATE_WAIT_MIN_PLAYER
	end
end

function RoomTableLogic.doaction(tableobj, request, seat)
	local player_action = tableobj.action_player_list[request.rid]

	local roomgamelogic = msghelper:get_game_logic()
	roomgamelogic.run(tableobj.gamelogic)
end

function RoomTableLogic.disconnect(tableobj, request, seat)
	seat.is_disconnected = true
	seat.is_tuoguan = false

	local noticemsg = {
		rid = seat.rid,
		roomsvr_seat_index = seat.index,	
		is_leave = true,
	}

	msghelper:sendmsg_to_alltableplayer("PlayerLeaveNtc", noticemsg)
	--TO ADD添加玩家掉线处理
    --将玩家站起
    ---测试需要将玩家站起后踢掉
    --local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")
    --roomtablelogic.passive_standuptable(tableobj, request, seat, EStandupReason.STANDUP_REASON_DISCONNECTED)
end

function RoomTableLogic.playerleave(tableobj, request, seat)
	local noticemsg = {
		rid = seat.rid,
		roomsvr_seat_index = seat.index,	
		is_leave = true,
	}
	if request.is_leave then
		noticemsg.is_leave = true
		seat.is_tuoguan = true
	else
		noticemsg.is_leave = false
		seat.is_tuoguan = false
	end

	msghelper:sendmsg_to_alltableplayer("PlayerLeaveNtc", noticemsg)	
end

function RoomTableLogic.leavetableallplayer(tableobj,reason)
	local roomseatlogic = logicmng.get_logicbyname("roomseatlogic")
	for _, seat in ipairs(tableobj.seats) do
		if not roomseatlogic.is_empty(seat) then
			RoomTableLogic.passive_standuptable(tableobj, nil, seat, reason)
			RoomTableLogic.passive_leavetable(tableobj, seat.rid, true)
		end
	end
end

function RoomTableLogic.get_svr_id(tableobj)
	return tableobj.svr_id
end

function RoomTableLogic.get_sitdown_player_num(tableobj)
	return tableobj.sitdown_player_num
end

function RoomTableLogic.get_ingame_playernum(tableobj)
	local playernum = 0
	local roomgamelogic = msghelper:get_game_logic()	
	for _, seat in ipairs(tableobj.seats) do
		if roomgamelogic.is_ingame(tableobj.gamelogic, seat) then
			playernum = playernum + 1
		end
	end
	return playernum
end

--- 根据指定桌位号获得一张空座位
-- @param tableobj 牌桌对象
-- @param index 座位号
function RoomTableLogic.get_emptyseat_by_index(tableobj, index)
	local roomseatlogic = logicmng.get_logicbyname("roomseatlogic")
	if index == nil or index <= 0 or index > tableobj.conf.max_player_num then
		for index, seat in pairs(tableobj.seats) do
			if roomseatlogic.is_empty(seat) then
				return seat
			end
		end
	else
		local seat = tableobj.seats[index]
		if roomseatlogic.is_empty(seat) then
			return seat
		end
	end
	return nil
end

function RoomTableLogic.get_seat_by_rid(tableobj, rid)
	if tableobj.seats == nil then
		return nil
	end

	for index, seat in pairs(tableobj.seats) do
		if rid == seat.rid then
			return seat
		end
	end
	return nil
end

--- 根据玩家的rid查找玩家是否在旁观队列
-- @param tableobj 牌桌对象
-- @param rid  玩家rid
function RoomTableLogic.get_waitplayer_by_rid(tableobj, rid)
	for index, waitplayer in pairs(tableobj.waits) do
		if rid == waitplayer.rid then
			return waitplayer
		end
	end
	return nil
end

--- 判断当前桌子是否坐满
-- @param tableobj  牌桌对象
function RoomTableLogic.is_full(tableobj)
	return (tableobj.sitdown_player_num >= tableobj.conf.max_player_num)
end

---判断当前是否能够开始游戏
-- @param tableobj	牌桌对象
function RoomTableLogic.is_canstartgame(tableobj)
	return RoomTableLogic.is_full(tableobj)
end

--- 判断游戏是否结束
-- @param tableobj 牌桌对象
function RoomTableLogic.is_gameend(tableobj)
	if tableobj.state == ETableState.TABLE_STATE_GAME_END
            or tableobj.state == ETableState.TABLE_STATE_WAIT_PLAYER_SITDOWN
            or tableobj.state == ETableState.TABLE_STATE_UNKNOW then
		return true
	end

	return false
end

--- 判断当前局是否已经结束游戏
-- @param tableobj 牌桌对象
function RoomTableLogic.is_onegameend(tableobj)
	if tableobj.state == ETableState.TABLE_STATE_ONE_GAME_END 
		or tableobj.state == ETableState.TABLE_STATE_ONE_GAME_REAL_END
		or tableobj.state == ETableState.TABLE_STATE_WAIT_ALL_READY then
		return true
	end

	if tableobj.state == ETableState.TABLE_STATE_WAIT_GAME_END 
		or tableobj.state == ETableState.TABLE_STATE_WAIT_ONE_GAME_REAL_END then
		return true
	end

	return RoomTableLogic.is_gameend(tableobj)
end
--- 玩家准备处理函数
-- @param tableobj 牌桌对象
-- @param request 请求数据
-- @param seat 桌位对象
function RoomTableLogic.gameready(tableobj, request, seat)
	if tableobj.state ~= ETableState.TABLE_STATE_WAIT_ALL_READY then
		return
	end
	local noticemsg = {
		rid = seat.rid,
		roomsvr_seat_index = seat.index,
		isready = 0,
		gameid = tableobj.conf.gameid,
	}
	if seat.state == ESeatState.SEAT_STATE_WAIT_START then 
		noticemsg.isready = 1 
	end
	
	msghelper:sendmsg_to_alltableplayer("GameReadyResultNtc", noticemsg)
	local isallready = true
	local playernum = 0
	local roomseatlogic = logicmng.get_logicbyname("roomseatlogic")
	for k, v in ipairs(tableobj.seats) do
		if not roomseatlogic.is_empty(v) then
			if v.state ~= ESeatState.SEAT_STATE_WAIT_START then
				isallready = false
				break
			end
			playernum = playernum + 1
		end
	end
	if isallready and playernum >= 2 and tableobj.state == ETableState.TABLE_STATE_WAIT_ALL_READY then
		---如果桌子状态是等待玩家开始的状态,且所有玩家都准备好了,则桌子进入开始倒计时状态
		if tableobj.conf.zuozhuang.key == 5 
			and RoomTableLogic.get_seat_by_rid(tableobj, tableobj.conf.create_user_rid) == nil then
			return
		end
		
		if tableobj.is_had_start then
			tableobj.state = ETableState.TABLE_STATE_ONE_GAME_START			
		else
			local waits = tabletool.deepcopy(tableobj.waits)
			for rid, waitinfo in pairs(waits) do
				RoomTableLogic.passive_leavetable(tableobj, rid, true)
			end
			tableobj.state = ETableState.TABLE_STATE_GAME_START
		end
		local roomgamelogic = msghelper:get_game_logic()
		roomgamelogic.run(tableobj.gamelogic)
		msghelper:report_table_state()
	end

end

function RoomTableLogic.sendGameStart(tableobj)
	local gamestartntcmsg = {}
	gamestartntcmsg.gameinfo = {}
	msghelper:copy_table_gameinfo(gamestartntcmsg.gameinfo)
	msghelper:sendmsg_to_alltableplayer("GameStartNtc", gamestartntcmsg)
end


function RoomTableLogic.setallseatstate(tableobj, state)
    for k,value in ipairs(tableobj.seats) do
        value.state = state
    end
end

--- 保存牌桌战绩记录
-- @param tableobj 桌子对象
--
function RoomTableLogic.saveGamerecords(tableobj)
	-- body
	filelog.sys_error("-------------saveGamerecords------------------",tableobj.conf.room_type,"++++++++++++",
		tableobj.gamerecords)
	if tableobj.conf.room_type == ERoomType.ROOM_TYPE_FRIEND_COMMON then
		if tableobj.gamerecords and type(tableobj.gamerecords) == "table" then
			for k,value in ipairs(tableobj.gamerecords) do
				local tablerecords = {}
				tablerecords.table_create_time = tableobj.conf.create_user_rid
				tablerecords.id = ""..tostring(tableobj.id)..tostring(tableobj.conf.create_user_rid)..tostring(value.rid)..tostring(timetool.get_time())
				tablerecords.table_id = tableobj.id
				tablerecords.table_create_time = tableobj.conf.create_time
				tablerecords.tablecreater_rid = tableobj.conf.create_user_rid
				tablerecords.rid = value.rid
				tablerecords.record = tabletool.deepcopy(tableobj.gamerecords)
				filelog.sys_error("-------------saveGamerecords------------------",tablerecords)
				playerdatadao.save_player_tablerecords("insert",value.rid,tablerecords)
			end
		end
	end
end
--- 向桌内的玩家广播聊天消息
-- @param tableobj
-- @param message
--
function RoomTableLogic.sendmessage(tableobj, request)
	local responmsg = {
		messages = request.messages,
		chat_type = request.chat_type,
		gameid = request.gameid,
	}
	msghelper:sendmsg_to_alltableplayer("PlayerMessageNtc",responmsg)
end


return RoomTableLogic